﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using WeChat.Common;
using System.Text;
using System.IO;
using BLL;
using System.Web.SessionState;
using System.Collections.Generic;
using WxPayAPI;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WeChat
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AjaxSets : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            string RtnMsg = "0";
            GetUserInfoBLL gb = new GetUserInfoBLL();
            string funcName = context.Request.Form["functionName"];
            string openid = context.Request.Form["openid"];

            switch (funcName)
            {
                case "unbind": //解绑

                    if (new BindIdBLL().delUser(openid))
                    {
                        if (context.Request.Cookies["CardId"] != null && context.Request.Cookies["UserName"] != null)
                        {
                            string CardId = HttpContext.Current.Server.UrlDecode(context.Request.Cookies["CardId"].Value);
                            string UserName = HttpContext.Current.Server.UrlDecode(context.Request.Cookies["UserName"].Value);
                            ApplicationLog.Write("当前用户Id：" + CardId + "Name：" + UserName + "解除绑定！");
                        }
                        else 
                        {
                            ApplicationLog.Write("当前用户未记住密码解除绑定！");
                        }

                        //清楚cookie
                        HttpCookie cookie = new HttpCookie("ddls");
                        cookie.Expires = DateTime.Now.AddDays(-1);//设置过期时间
                        HttpContext.Current.Response.Cookies.Add(cookie);
                        HttpCookie cookie1 = new HttpCookie("Name");
                        cookie1.Expires = DateTime.Now.AddDays(-1);//设置过期时间
                        HttpContext.Current.Response.Cookies.Add(cookie1);
                        HttpCookie cookie2 = new HttpCookie("PassWord");
                        cookie2.Expires = DateTime.Now.AddDays(-1);//设置过期时间
                        HttpContext.Current.Response.Cookies.Add(cookie2);
                        RtnMsg = "1";

                    }
                    break;
                case "torage":
                    string opid = context.Request.Form["opid"];
                    RtnMsg = JsonConvert.SerializeObject(BindIdBLL.Torage(opid));
                    break;
                case "getcanteen":
                    string orderid = context.Request.Form["orderid"];
                    RtnMsg = JsonConvert.SerializeObject(BindIdBLL.getcanteen(orderid));
                    break;
                case "get_orderdetail":
                    orderid = context.Request.Form["orderid"];
                    JObject jo = new JObject();
                    jo.Add("baseInfo", JsonConvert.SerializeObject(BindIdBLL.get_orderdetail(orderid)));
                    RtnMsg = jo.ToString();

                    break;
                case "reportLoss"://挂失
                    string cardNum = context.Request.Form["cardNum"];
                    string cardState = context.Request.Form["state"];
                    if (cardState == "32")
                    {
                        gb.changeCardState("96", cardNum);
                        if (new LoginBLL().IsOnline())//联机
                        {

                            RtnMsg = "3";
                        }
                        else
                        {
                            RtnMsg = "1";
                        }
                    }
                    break;
                case "lifted"://解挂
                    cardNum = context.Request.Form["cardNum"];
                    cardState = context.Request.Form["state"];
                    if (cardState == "96")
                    {
                        gb.changeCardState("32", cardNum);
                        if (new LoginBLL().IsOnline())//联机
                        {

                            RtnMsg = "4";
                        }
                        else
                        {
                            RtnMsg = "2";
                        }
                    }
                    break;
                case "IsValue":
                    //498接口微信支付
                    //var IsValue =context.Session["IsValue"].ToString();

                    //官方微信接口微信支付
                    Log.Info(this.GetType().ToString(), "page load");

                    NativePay nativePay = new NativePay();

                    //生成扫码支付模式一url
                    //string url1 = nativePay.GetPrePayUrl("123456789");

                    //生成扫码支付模式二url
                    string Opid = context.Session["openid"].ToString();
                    string money = context.Session["moneys"].ToString();
                    string url2 = nativePay.GetPayUrl(Opid, money);


                    //浏览器拉起微信支付
                    //Log.Info(this.GetType().ToString(), "page load");
                    H5Pay h5Pay = new H5Pay();
                    string scip = GetIP();//获取客户端真实IP
                                          //string money = context.Session["moneys"].ToString();
                    string url = h5Pay.GetPayUrl(scip, Opid, money);//通过统一下单接口进行H5支付

                    RtnMsg = url2;
                    break;
                case "Return":
                    //   string o = context.Session["o"].ToString();
                    //   string tn =context.Session["tn"].ToString();
                    //   //此处自行验证各值合法性
                    //   string json = "{\"a\":\"payChk\",\"o\":\"" + o + "\",\"tn\":\"" + tn + "\"}";
                    //   string returnTxt = RsaDemo.Common.HttpPost("s=" + json);
                    //   IDictionary<string, object> rlt = RsaDemo.Common.JsonToDictionary(returnTxt);
                    //   if (rlt["f"].ToString() == "1")
                    //   {
                    //IDictionary<string, object> d = (IDictionary<string, object>)rlt["d"];
                    ////this.richTextBox1.Text = d["k"].ToString();
                    //RtnMsg = d["f"].ToString();
                    ////this.textBox5.Text = "0001";
                    //   }
                    //   else
                    //   {
                    ////MessageBox.Show(rlt["m"].ToString());
                    //   }

                    string q = context.Session["q"].ToString();
                    if (q == "2")
                    {
                        RtnMsg = "微信";
                    }
                    else if (q == "3")
                    {
                        RtnMsg = "百度百付宝";
                    }
                    else if (q == "4")
                    {
                        RtnMsg = "苏宁易付宝";
                    }
                    else if (q == "6")
                    {
                        RtnMsg = "京东钱包";
                    }
                    else if (q == "7")
                    {
                        RtnMsg = "QQ钱包";
                    }
                    else if (q == "8")
                    {
                        RtnMsg = "翼支付";
                    }
                    break;
                default:
                    break;
            }

            //接收ajax的get请求
            //string fcnName = HttpContext.Current.Request.QueryString["functionName"];
            //if (fcnName.Equals("unbind"))
            //{
            //    string openid = HttpContext.Current.Request.QueryString["openid"];
            //    ApplicationLog.Write("ajax接收参数" + fcnName + "---" + openid);
            //}
            context.Response.ContentType = "text/plain";
            context.Response.Write(RtnMsg);
        }
        private string GetLocalIPAddress()
        {
            System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            string strNativeIP = "";
            string strServerIP = "";
            if (addressList.Length > 1)
            {
                strNativeIP = addressList[0].ToString();
                strServerIP = addressList[1].ToString();
            }
            else if (addressList.Length == 1)
            {
                strServerIP = addressList[0].ToString();
            }
            return strServerIP;
        }
        //因H5支付要求商户在统一下单接口中上传用户真实ip地址“spbill_create_ip”，故需要调用以下方法。
        public string GetIP()
        {
            HttpRequest request = HttpContext.Current.Request;
            string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = request.ServerVariables["REMOTE_ADDR"];
            }
            if (string.IsNullOrEmpty(result))
            {
                result = request.UserHostAddress;
            }
            if (string.IsNullOrEmpty(result))
            {
                result = "0.0.0.0";
            }
            return result;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
